<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | ExamController.php v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : rahul
// | Date : 2018/10/30
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;


use app\core\common\bean\BeanController;
use app\core\common\bean\BFC;
use app\core\model\CronTask;

class CronController extends SiteBaseController {
    use BeanController;

    const STATE_PAUSE = 0;
    const STATE_NORMAL = 1;
    const CAN_NOT_OVERLAP = 0;
    const CAN_OVERLAP = 1;

    public static $STATES = [
        self::STATE_PAUSE => '暂停',
        self::STATE_NORMAL => '正常运行'
    ];

    public static $OVERLAPS = [
        self::CAN_NOT_OVERLAP => '否',
        self::CAN_OVERLAP => '是'
    ];

    /**
     * 获取bean名称
     *
     * @return string
     */
    protected function _getBeanName() {
        return "cron_task";
    }

    protected function _enableEid() {
        return false;
    }

    protected function _isSoftDelete() {
        return false;
    }

    protected function _getBeanFieldsConfig() {
        $configs = $this->_getDefaultBeanFieldsConfig();

        $configs['id'][BFC::HIDE] = true;
        $configs['name'][BFC::WIDTH] = 200;
        $configs['module'][BFC::WIDTH] = 200;
        $configs['state'][BFC::WIDTH] = 100;
        $configs['cron'][BFC::WIDTH] = 150;
        $configs['title'][BFC::MIN_WIDTH] = 200;
        $configs['timeout'][BFC::WIDTH] = 100;
        $configs['overlap'][BFC::WIDTH] = 100;

        return $configs;
    }

    protected function _getCellMinWidth() {
        return 100;
    }

    protected function _rowPreProcess(&$row) {
        $row['state'] = fetch(self::$STATES, $row['state'], '');
        $row['overlap'] = fetch(self::$OVERLAPS, $row['overlap'], '');
    }

    protected function _getRowBtns() {
        return [
            'edit' => [
                BFC::TITLE => '编辑',
                BFC::ICON => 'edit',
                BFC::ACTION => '/core/site.cron/edit?id=' . self::getParamPlaceholder("id"),
                BFC::DLG => [
                    BFC::DLG_RELOAD => BFC::RELOAD_ROW,
                    BFC::DLG_WIDTH => '60vw',
                    BFC::DLG_HEIGHT => '80vh'
                ]
            ],
            'exec' => [
                BFC::TITLE => '手动运行',
                BFC::ICON => 'release',
                BFC::ACTION => '/core/site.cron/exec?id=' . self::getParamPlaceholder("id"),
                BFC::DLG => [
                    BFC::DLG_WIDTH => '60vw',
                    BFC::DLG_HEIGHT => '80vh'
                ],
                BFC::CLAZZ => 'layui-bg-blue'
            ],
            'log' => [
                BFC::TITLE => '运行日志',
                BFC::ICON => 'log',
                BFC::ACTION => '/core/site.cron_log/all?id=' . self::getParamPlaceholder("id"),
                BFC::DLG => [
                    BFC::DLG_WIDTH => '60vw',
                    BFC::DLG_HEIGHT => '80vh'
                ],
                BFC::CLAZZ => 'layui-bg-orange'
            ]
        ];
    }

    /**
     * 编辑cron任务页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit() {
        $id = input('get.id');
        $task = CronTask::getById($id);
        $this->assign('task', $task);
        return view();
    }

    /**
     * 手动执行cron任务页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exec() {
        $id = input('get.id');
        $task = CronTask::getById($id);
        $task['state'] = self::$STATES[$task['state']];
        $task['overlap'] = self::$OVERLAPS[$task['overlap']];
        $this->assign('task', $task);
        return view();
    }

    /**
     * 更新任务
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update() {
        $id = input('post.id');
        $title = input('post.title');
        $state = input('post.state');
        $cron = input('post.cron');
        $timeout = input('post.timeout');
        $overlap = input('post.overlap');

        CronTask::updateTask($id, $title, $state, $cron, $timeout, $overlap);
        $this->success('保存成功！');
    }
}